package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Database.StaffDB;
import Model.Approval;
import Model.Committee;
import Model.CommitteeRole;

/**
 * Servlet implementation class CommitteeTable
 */

public class CommitteeTable extends HttpServlet {
	private static final long serialVersionUID = 1L;
	JspWriter out;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommitteeTable() {
		super();
		// TODO Auto-generated constructor stub
	}

	public CommitteeTable(JspWriter outputStream) {
		// TODO Auto-generated constructor stub
		out = outputStream;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		ArrayList<Committee> commTable = new ArrayList<Committee>();
		commTable = Database.CommitteeDB.getAllCommittees();

		ArrayList<CommitteeRole> commRole = new ArrayList<CommitteeRole>();

		for (int i = 0; i < commTable.size(); i++) {
			out.print("<tr id=\"commTable2\" class='"+commTable.get(i).getId()+"'>");
			out.print("<td>");
			out.print(commTable.get(i).getTitle());
			out.print("</td>");

			out.print("<td>");
			out.print(commTable.get(i).getPurpose());
			out.print("</td>");

			commRole = Database.CommitteeRoleDB.getCommitteeRole(commTable.get(
					i).getId());
			out.print("<td>");
			for (int j = 0; j < commRole.size(); j++) {
				out.print(commRole.get(j).getName() + "&nbsp-&nbsp");
				String[] arr = commRole.get(j).getStaffName().split(", ");
				for (int k = 0; k < arr.length; k++) {
					if (k == arr.length - 1) {
						out.print(Database.StaffDB.getStaffNamebyId(arr[k]));
					} else {
						out.print(Database.StaffDB.getStaffNamebyId(arr[k])
								+ ", ");
					}
				}
				out.print("<br>");
			}
			out.print("</td>");
			
			out.print("<td>");
			out.print(formatdate(commTable.get(i).getDate()));
			
			out.print("</td>");

			out.print("<td>");
			out.print("<a href=\"committee_modifycomm.jsp?action=e&id="
					+ commTable.get(i).getId()
					+ "\">Edit</a>/<a href=\"committee_modifycomm.jsp?action=d&id="
					+ commTable.get(i).getId() + "\">Delete</a>");
			out.print("</td>");

			out.print("<td>");
			out.print("<a target=\"_blank\" href=\"CommitteePrint?id=" + commTable.get(i).getId()
					+ "\">Print</a>");
			out.print("</td>");

			out.print("</tr>");
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}
	
}
